<template>
  <teleport to="body">
    <div 
      v-if="visible"
      :class="[
        'bx-dialog',
        'bx-glass'
      ]"
      :style="{ zIndex }"
    >
      <!-- 遮罩层 -->
      <div 
        v-if="mask"
        class="bx-dialog__mask"
        @click="handleMaskClick"
      ></div>

      <!-- 对话框内容 -->
      <div 
        :class="[
          'bx-dialog__wrapper',
          {
            'bx-dialog__wrapper--centered': centered
          }
        ]"
      >
        <div 
          :class="[
            'bx-dialog__content',
            'bx-glass'
          ]"
          :style="dialogStyle"
        >
          <!-- 头部 -->
          <div 
            v-if="title || $slots.title || closable"
            class="bx-dialog__header"
          >
            <div class="bx-dialog__title">
              <slot name="title">{{ title }}</slot>
            </div>
            
            <button
              v-if="closable"
              class="bx-dialog__close"
              @click="handleClose"
            >
              <BxIcon name="x" />
            </button>
          </div>

          <!-- 主体内容 -->
          <div class="bx-dialog__body">
            <slot></slot>
          </div>

          <!-- 底部 -->
          <div 
            v-if="$slots.footer || showDefaultFooter"
            class="bx-dialog__footer"
          >
            <slot name="footer">
              <BxSpace>
                <BxButton @click="handleCancel">{{ cancelText }}</BxButton>
                <BxButton 
                  type="primary" 
                  :loading="confirmLoading"
                  @click="handleOk"
                >
                  {{ okText }}
                </BxButton>
              </BxSpace>
            </slot>
          </div>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script>
import { computed, watch, onMounted, onUnmounted } from 'vue'
import BxIcon from '../basic/BxIcon.vue'
import BxButton from '../basic/BxButton.vue'
import BxSpace from '../layout/BxSpace.vue'

export default {
  name: 'BxDialog',
  components: {
    BxIcon,
    BxButton,
    BxSpace
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: 520
    },
    centered: {
      type: Boolean,
      default: false
    },
    closable: {
      type: Boolean,
      default: true
    },
    mask: {
      type: Boolean,
      default: true
    },
    maskClosable: {
      type: Boolean,
      default: true
    },
    destroyOnClose: {
      type: Boolean,
      default: false
    },
    zIndex: {
      type: Number,
      default: 1000
    },
    okText: {
      type: String,
      default: '确定'
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    confirmLoading: {
      type: Boolean,
      default: false
    },
    showDefaultFooter: {
      type: Boolean,
      default: true
    }
  },
  emits: ['update:visible', 'ok', 'cancel', 'close', 'open'],
  setup(props, { emit }) {
    const dialogStyle = computed(() => {
      const style = {}
      
      if (props.width) {
        const width = typeof props.width === 'number' ? `${props.width}px` : props.width
        style.width = width
        style.maxWidth = '90vw'
      }
      
      return style
    })

    const handleOk = () => {
      emit('ok')
    }

    const handleCancel = () => {
      emit('update:visible', false)
      emit('cancel')
    }

    const handleClose = () => {
      emit('update:visible', false)
      emit('close')
    }

    const handleMaskClick = () => {
      if (props.maskClosable) {
        handleClose()
      }
    }

    const handleEscapeKey = (event) => {
      if (event.key === 'Escape' && props.visible) {
        handleClose()
      }
    }

    watch(() => props.visible, (newVal) => {
      if (newVal) {
        emit('open')
        // 禁止页面滚动
        document.body.style.overflow = 'hidden'
      } else {
        // 恢复页面滚动
        document.body.style.overflow = ''
      }
    })

    onMounted(() => {
      document.addEventListener('keydown', handleEscapeKey)
    })

    onUnmounted(() => {
      document.removeEventListener('keydown', handleEscapeKey)
      // 确保恢复页面滚动
      document.body.style.overflow = ''
    })

    return {
      dialogStyle,
      handleOk,
      handleCancel,
      handleClose,
      handleMaskClick
    }
  }
}
</script>

<style scoped>
.bx-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
}

.bx-dialog__mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(4px);
  animation: bx-dialog-mask-fade-in 0.3s ease-out;
}

.bx-dialog__wrapper {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 20px;
  overflow-y: auto;
}

.bx-dialog__wrapper--centered {
  align-items: center;
}

.bx-dialog__content {
  position: relative;
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur-strong);
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-large);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 80px);
  animation: bx-dialog-zoom-in 0.3s ease-out;
}

.bx-dialog__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--bx-glass-border);
  flex-shrink: 0;
}

.bx-dialog__title {
  font-size: 16px;
  font-weight: 600;
  color: var(--bx-text-primary);
  margin: 0;
}

.bx-dialog__close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--bx-text-secondary);
  cursor: pointer;
  border-radius: var(--bx-radius-small);
  transition: all var(--bx-transition-medium);
}

.bx-dialog__close:hover {
  background: var(--bx-glass-bg-hover);
  color: var(--bx-text-primary);
}

.bx-dialog__body {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  font-family: var(--bx-font-family);
  color: var(--bx-text-primary);
  line-height: 1.6;
}

.bx-dialog__footer {
  padding: 16px 24px;
  border-top: 1px solid var(--bx-glass-border);
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.02);
  display: flex;
  justify-content: flex-end;
}

/* 动画效果 */
@keyframes bx-dialog-mask-fade-in {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes bx-dialog-zoom-in {
  0% {
    opacity: 0;
    transform: scale(0.8);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-dialog__wrapper {
    padding: 20px 16px;
  }
  
  .bx-dialog__content {
    width: 100% !important;
    max-width: 100% !important;
    max-height: calc(100vh - 40px);
    margin: 0;
  }
  
  .bx-dialog__header {
    padding: 16px 20px;
  }
  
  .bx-dialog__title {
    font-size: 15px;
  }
  
  .bx-dialog__body {
    padding: 20px;
  }
  
  .bx-dialog__footer {
    padding: 12px 20px;
  }
  
  .bx-dialog__close {
    width: 28px;
    height: 28px;
  }
}
</style>

